package com.demo.common;

import com.demo.common.RandomKit;
import com.demo.common.RoutDb;
import com.demo.common.RoutDbKit;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.kit.JMap;
import com.jfinal.template.Engine;
import com.jfinal.template.Template;

public class RoutDbInterceptor implements Interceptor {

    public void intercept(Invocation inv) {
        //方法级别的注解
        RoutDb routDb = inv.getMethod().getAnnotation(RoutDb.class);
        // 优先方法上的注解，没有时从类上获取
        if (routDb == null) {
            routDb = inv.getTarget().getClass().getAnnotation(RoutDb.class);
        }
        // 没有注解直接跳出
        if (routDb == null) {
            inv.invoke();
            return;
        }
        try {
            String dbName = EngineKit.getDbName(routDb);
            RoutDbKit.setDbName(dbName);
            inv.invoke();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            RoutDbKit.removeDb();
        }
    }
}
